package cn.tedu.hongpei.controller;

import cn.tedu.hongpei.Exception.BusinessException;
import cn.tedu.hongpei.mapper.PictureMapper;
import cn.tedu.hongpei.pojo.Dto.PictureDTO;
import cn.tedu.hongpei.pojo.Picture;
import cn.tedu.hongpei.response.AppCode;
import cn.tedu.hongpei.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;
/**
 * @author coinyang
 * @date 2023/3/31 11:53
 */
@RestController
@RequestMapping("/picture")
@Api(tags = "图片管理")
@Slf4j
public class PictureController {
    @Autowired
    PictureMapper pictureMapper;


    @Value("${imagePath}")
    private String imagePath;

    @Value("${url}")
    private String url;



    @PostMapping("fileUpload")
    @ApiOperation("文件上传")
    public String fileUpload(MultipartFile file) {
        String datePath = "";
        String fileName = "";
        try {
            fileName = file.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            fileName = UUID.randomUUID() + suffix;
            datePath = new SimpleDateFormat("/yyyy/MM/dd/").format(new Date());
            File dirFile = new File(url + datePath);
            //判断文件夹不存在 则创建
            if (!dirFile.exists()) {
                dirFile.mkdirs();
            }
            //保存文件到指定文件夹  异常抛出
            file.transferTo(new File(url + datePath + fileName));
        } catch (Exception e) {
            throw new BusinessException(AppCode.FILE_UPLOAD_FAIL);
        }
        String urlPath = datePath+fileName;
        return urlPath;



    }

    @ApiOperation("图片上传")
    @PostMapping("insert")
    public int insert(@RequestBody PictureDTO pictureDTO){
        log.debug("开始处理图片上传:{}",pictureDTO);
        if (pictureDTO.getUrl()==""){
            throw new BusinessException(AppCode.PIC_NOTFOUND);
        }
        Picture picture = new Picture();

        //复制属性
        BeanUtils.copyProperties(pictureDTO,picture);
        picture.setUrl(pictureDTO.getUrl());
        picture.setDescription(pictureDTO.getDescription());
        picture.setGmtCreate(new Date());
        pictureMapper.insert(picture);
        log.debug("插入数据成功：{}",picture);
        return 1;
    }

    @ApiOperation("删除图片")
    @PostMapping ("/delete/{id:[0-9]+}")
    public String delete(@PathVariable Long id) {
        log.debug("开始处理【删除图片】的请求，参数：{}", id);
        Picture  picture = pictureMapper.selectById(id);
        if (picture == null){
            throw new BusinessException("图片不存在");
        }
        int rows = pictureMapper.deleteById(id);
        if (rows>=1){
            new File(url+picture.getUrl()).delete();
        }
        return "删除图片数为："+rows;
    }

    @ApiOperation("修改图片信息")
    @PostMapping("/update/{id:[0-9]+}")
    public String update(@PathVariable Long id, PictureDTO pictureDTO) {
        log.debug("开始处理【修改图片】的请求，参数：{}", id);
        Picture picture = pictureMapper.selectById(id);
        log.debug("查询出来的数据为：{}",picture);
        if (picture == null){
            throw new BusinessException("图片不存在");
        }
        picture.setUrl(pictureDTO.getUrl());
        picture.setDescription(pictureDTO.getDescription());
        log.debug("准备修改后的的数据为：{}",picture);
        int rows = pictureMapper.update(picture);
        return "修改图片数为："+rows;
    }


    @ApiOperation("查询图片合集")
    @PostMapping("/select")
    public ArrayList<Picture> selectList() {
        log.debug("开始处理【查询图片】的请求，参数：{}");
        ArrayList<Picture> list= pictureMapper.count();
        log.debug("查询出来的结果为：{}",list);
        return list;
    }
}
